package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //注入口味表service
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishMapper dishMapper;

    @Override
    @Transactional //事务管理
    public void saveFlavor(DishDto dishDto) {
        //调用自己的方法 保存基本属性
        this.save(dishDto);
        //获取dto里面的集合 添加到口味表里面
        List<DishFlavor> flavors = dishDto.getFlavors();
        //当前集合里面的数据dishId没有值  要给他赋值
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        //调用口味表添加口味数据
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> pageDishDto(Integer page, Integer pageSize, String name) {
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        Page<DishDto> dtoPage = dishMapper.pageDishDto(dishDtoPage, name);
        return dtoPage;
    }

    /**
     * 根据ID查询数据
     *
     * @param id
     * @return
     */
    @Override
    public DishDto updateFlavor(Long id) {
        DishDto dishDto = dishMapper.updateFlavor(id);
        return dishDto;
    }

    /**
     * 修改
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateDishById(DishDto dishDto) {
        //修改菜品的数据
        this.updateById(dishDto);
        //根据菜品ID 删除 口味表 dish_id的数据
        dishFlavorService.update().eq("dish_id",dishDto.getId()).remove();
        //添加口味表数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //添加dishID属性
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询菜品的详情信息
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {

        return dishMapper.listDishDto(categoryId,name);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteDish(Long[] ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus()==1) {
                throw new CustomerException("该菜品正在售卖状态中");
            }
            dishMapper.deleteById(id);
        }
    }
}


